package tempEngine.network.hostSide;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import tempEngine.network.SocketThread;
import tempEngine.network.protocols.enums.RoleRequest;
import tempEngine.network.protocols.exceptions.NoFreeSlotException;
import tempEngine.network.protocols.exceptions.UnKnownRoleException;

public class HostSocketThread extends SocketThread {
	private HostGameHandler _hostGame;
	
	public HostSocketThread(Socket s, HostGameHandler hostGame) 
				throws UnknownHostException, IOException {
		super(s);
		_hostGame = hostGame;
	}

	

	@Override
	protected void preRunLoopHandling() throws IOException, UnKnownRoleException {
		// TODO read the Distant Client Type : Player Spectator
		// use the enum class for this purpose 
		//first read the incomming line :
		RoleRequest role = RoleRequest.factory(_in.readLine());
		switch (role) {
		case PLAYER:
			try {
				_hostGame.addAsPlayer(this);
			} catch (NoFreeSlotException e) {
				e.printStackTrace();
				//TODO send an noFreeSlot answer and delete the stackTrace
			}
			//TODO set the communication protocol to SinglePoint for receiving
			break;
		case SPECTATOR:
			try {
				_hostGame.addAsSpectator(this);
			} catch (NoFreeSlotException e) {
				e.printStackTrace();
				//TODO send an noFreeSlot answer and delete the stackTrace
			}
			//TODO set the communication protocol to none for receiving
			break;
		default:
			throw new UnKnownRoleException();
		}
	}

	@Override
	protected void runLoopHandling() {
		// TODO read new incomming coord using the correct handler 
		//version : SinglePointProtocol.readPoint() or 
		//DoublePointProtocol.readPoint()
		// create 3 classes common for this with static function to 
		//send or receive this informations
		//this thread should only receive in SinglePoint mode and send in DoublePoint
	}
	
	@Override
	protected void postRunLoopHandling() {
		//nothing to do here for now
	}
}
